﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using mfXmlSerializer.TestObjects;
using System.IO;
using System.Threading;
using System.Globalization;
using System.Diagnostics;

namespace mfXmlSerializer.Test
{
    [TestClass]
    public class SerializationMapTests
    {
        [TestMethod]
        public void Serialize_Valid_SimpleParameterObject()
        {
            TestSetUps.SetupDateTimeTests();

            var para = TestDataFactory.CreateSimpleCompleteParameter();

            var serPara = new mfXmlTypedSerializer(para.GetType());
            String curXml = serPara.Serialize(para);

            String refXml = TestDataFactory.CreateSimpleCompleteParameterString();

            Console.WriteLine("ref xml: \t" + refXml);
            Console.WriteLine("cur xml: \t" + curXml);

            Assert.AreEqual(refXml, curXml);
        }

        [TestMethod]
        [ExpectedException(typeof(mfXmlException))]
        public void Serialize_Invalid_SimpleParameterObject_WithLogging_AsType2Serialize()
        {
            //needed 4 Testing
            Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");

            var para = TestDataFactory.CreateSimpleCompleteParameter();

            var serPara = new mfXmlTypedSerializer(para.LogConfig.GetType());
            String curXml = serPara.Serialize(para);

        }

        [TestMethod]
        [ExpectedException(typeof(mfXmlException))]
        public void mfMapCreationElement_Name_NULL()
        {
            var element = new mfMapCreationElement(null);
        }       
        
        [TestMethod]
        [ExpectedException(typeof(mfXmlException))]
        public void mfMapCreationElement_Name_Empty()
        {
            var element = new mfMapCreationElement(String.Empty);
        }

        [TestMethod]
        public void XmlDefaultHeaderString() {

            var header = "version=\"1.0\" encoding=\"utf-8\"";

            var para = TestDataFactory.CreateSimpleCompleteParameter();
            var serPara = new mfXmlTypedSerializer(para.GetType());

            var cur = serPara.XmlHeader;

            Console.WriteLine("ref header: " + header);
            Console.WriteLine("cur header: " + cur);

            Assert.AreEqual(header, cur);
        }

        [TestMethod]
        public void Serialize_Valid_SimpleParameterObject_WithVersionNULL()
        {
            TestSetUps.SetupDateTimeTests();

            var para = TestDataFactory.CreateParameter_WithVersionNULL();

            var serPara = new mfXmlTypedSerializer(para.GetType());
            String curXml = serPara.Serialize(para);

            String refXml = TestDataFactory.CreateParameterString_WithMissingVersionElement();

            Console.WriteLine("ref xml: \t" + refXml);
            Console.WriteLine("cur xml: \t" + curXml);

            Assert.AreEqual(refXml, curXml);
        }

        [TestMethod]
        public void Serialize_Valid_SimpleParameterObject_WithEmptyVersionElement()
        {
            TestSetUps.SetupDateTimeTests();

            var para = TestDataFactory.CreateParameter_WithVersionNULL();

            var serPara = new mfXmlTypedSerializer(para.GetType());
            String curXml = serPara.Serialize(para);

            String refXml = TestDataFactory.CreateParameterString_WithMissingVersionElement();

            Console.WriteLine("ref xml: \t" + refXml);
            Console.WriteLine("cur xml: \t" + curXml);

            Assert.AreEqual(refXml, curXml);
        }

        [TestMethod]
        public void Serialize_Valid_SimpleParameterObject_WithLoggingNULL()
        {
            TestSetUps.SetupDateTimeTests();

            var para = TestDataFactory.CreateParameter_WithLoggingNULL();

            var serPara = new mfXmlTypedSerializer(para.GetType());
            String curXml = serPara.Serialize(para);

            String refXml = TestDataFactory.CreateParameterString_WithMissingLoggingElement();

            Console.WriteLine("ref xml: \t" + refXml);
            Console.WriteLine("cur xml: \t" + curXml);

            Assert.AreEqual(refXml, curXml);
        }

        [TestMethod]
        public void Serialize_Valid_RSSSample()
        {
            TestSetUps.SetupDateTimeTests();

            var rss = TestDataFactory.CreateRSSSample_Complete();

            var serPara = new mfXmlTypedSerializer(rss.GetType());

            //hang in rssVersionWriterAction in serialzation-process
            serPara.ElementSerialized += mfWriteRSSVersionAction.WriteRSSVersionAction;

            String curXml = serPara.Serialize(rss);

            String refXml = TestDataFactory.CreateRSSSampleString_Complete();

            Console.WriteLine("ref xml: \t" + refXml);
            Console.WriteLine("cur xml: \t" + curXml);

            Assert.AreEqual(refXml, curXml);
        }
    }
}
